package com.huang.springboot.controller;

import com.huang.springboot.pojo.Department;
import com.huang.springboot.pojo.Employee;
import com.huang.springboot.service.DepartmentService;
import com.huang.springboot.service.EmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Collection;

/**
 * @author yili_gi
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeServiceImpl;
    @Autowired
    private DepartmentService departmentServiceImpl;

    @ApiOperation("查询全体雇员接口")
    @GetMapping("/emp/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeServiceImpl.getEmployees();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    @GetMapping("/emp/add")
    public String getAddPage(Model model){
        Collection<Department> departments = departmentServiceImpl.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @ApiOperation("添加雇员接口")
    @PostMapping("/emp/add")
    public String addMethod(Employee employee){
        employeeServiceImpl.addEmployee(employee);
        return "redirect:/emp/emps";
    }

    @GetMapping("/emp/update/{id}")
    public String getUpdatePage(@PathVariable("id") Integer id, Model model){
        Employee employee = employeeServiceImpl.getEmployeeById(id);
        model.addAttribute("employee",employee);
        Collection<Department> departments = departmentServiceImpl.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    @ApiOperation("修改雇员信息接口")
    @PostMapping("/emp/update")
    public String update(Employee employee){
        employeeServiceImpl.updateEmployee(employee);
        return "redirect:/emp/emps";
    }

    @ApiOperation("删除雇员信息接口")
    @GetMapping("/emp/remove/{id}")
    public String remove(@PathVariable("id") Integer id){
        employeeServiceImpl.removeEmployee(id);
        return "redirect:/emp/emps";
    }


}
